package controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import common.Constant;
import dao.MonHocDAO;
import model.MonHocModel;
import util.StringUtil;

public class MonHocController extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MonHocController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String actionType = request.getParameter("actionType");
		
		if(actionType.equalsIgnoreCase("capnhat")){
			updateMonHocByMaMH(request, response);
		}
		else if(actionType.equalsIgnoreCase("themmoi")){
			insertMonHoc(request, response);
		}
		else if(actionType.equalsIgnoreCase("monHoc"))
		{
			MonHoc(request,response);
		}
	}	
	
	
	private void updateMonHocByMaMH(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		MonHocModel monHocModel=new MonHocModel();
		monHocModel.setMaMH(request.getParameter("txtMaMonHoc").trim());
		monHocModel.setTenMH(StringUtil.toUTF8(request.getParameter("txtTenMonHoc").trim()));
		monHocModel.setChungChi(request.getParameter("txtChungChi").trim());
		monHocModel.setLePhi(request.getParameter("txtLePhi").trim());
				
		if(MonHocDAO.updateMonHocByMaMH(monHocModel))
			response.sendRedirect("danhsachmonhoc.jsp?errUpdate=false"+request.getParameter("txtProLink"));
		else
			response.sendRedirect("danhsachmonhoc.jsp?errUpdate=true"+request.getParameter("txtProLink"));
			
	}
	
	private void insertMonHoc(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		MonHocModel monHocModel=new MonHocModel();
		monHocModel.setMaMH(request.getParameter("txtMaMonHoc").trim());
		monHocModel.setTenMH(StringUtil.toUTF8(request.getParameter("txtTenMonHoc").trim()));
		monHocModel.setChungChi(request.getParameter("txtChungChi").trim());
		monHocModel.setLePhi(request.getParameter("txtLePhi").trim());
		
		if(MonHocDAO.insertMonHoc(monHocModel))
			response.sendRedirect("danhsachmonhoc.jsp?errInsert=false"+request.getParameter("txtProLink"));
		else
			response.sendRedirect("danhsachmonhoc.jsp?errInsert=true"+request.getParameter("txtProLink"));
			
	}
	
	protected void MonHoc(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String maMonHoc = "";
		String pageNext = "danhsachmonhoc.jsp?MaMH=" + maMonHoc ;
		RequestDispatcher rd = request.getRequestDispatcher(pageNext);	
		
		ArrayList<MonHocModel> monHocModelList1  = new ArrayList<MonHocModel>();
		ArrayList<MonHocModel> monHocModelList2  = new ArrayList<MonHocModel>();
		if(request.getSession().getAttribute("monHocList") != null)
			monHocModelList1 = (ArrayList<MonHocModel>)request.getSession().getAttribute("monHocList");
		
		for(Integer i = 1; i <= monHocModelList1.size(); i++){
			
			MonHocModel monHocModel = monHocModelList1.get(i-1);
			
			if(request.getParameter("xoaMH") != null && request.getParameter("xoaMH").equals("xoa") && request.getParameter("chk" + i.toString()) != null)
			{
				if(monHocModel.getMaMH() != null){
					MonHocDAO.deleteMonHoc(monHocModel.getMaMH());
				}
			}
			else{
				monHocModelList2.add(monHocModel);
			}
		}
		
		if(request.getParameter("xoaMH").equals("xoa"))
		{
			
			rd = request.getRequestDispatcher(pageNext);	
			pageNext += "?xoa=DeleteTC";
		}
		request.setAttribute(Constant.MON_HOC_MODEL_LIST, monHocModelList2);
		rd.forward(request, response);
	}
}
